package com.vk.work2pdfdemo.controller;

import com.vk.work2pdfdemo.entity.Enterprises;
import com.vk.work2pdfdemo.entity.Levy;
import com.vk.work2pdfdemo.utils.ExcelCreateContract;
import com.vk.work2pdfdemo.utils.WordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangKai
 * 2024年02月06日 15:28
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/createContractsController")
public class CreateContractController {

    /**
     * @param file 生成文件
     * @param outputDir 生成地址
     */
    @PostMapping("/importEexcl")
    public StringBuilder importEexcl(MultipartFile file, String outputDir) {
        // 解析表格,将数据放入对象中
        ExcelCreateContract excelCreateContract = new ExcelCreateContract();
        Map<String, Object> map = excelCreateContract.excelInput(file);
        // 批量生成合同
        return createContracts(map, outputDir);
    }


    /**
     * 批量生成合同
     *
     * @param map 要生成的合同数据
     * @param outputDir 导出的地址
     * @return {@link StringBuilder}
     */
    public StringBuilder createContracts(Map<String, Object> map, String outputDir) {
        List<Enterprises> contractTemplates = (List<Enterprises>) map.get("ContractTemplates");
        Levy levy = (Levy) map.get("Levy");
        // 调用word文档工具类
        WordUtil wordUtil = new WordUtil();
        // 模板文件存放的目录
        String path = this.getClass().getClassLoader().getResource("").getPath();
        wordUtil.setBaseDir(path);
        // word生成的输出目录
        wordUtil.setOutputDir(outputDir);
        // 初始化数据map
        Map<String,Object> dataMap = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder("合同生成成功,生成地址: ");
        contractTemplates.forEach(x -> {
            // 企业名称
            wordUtil.setEnterpriseName(x.getEnterpriseName());
            // 合同名称
            wordUtil.setContractName("委托服务合同");
            // 录入基本数据
            dataMap.put("enterpriseName", x.getEnterpriseName());
            dataMap.put("actualOfficeAddress", x.getActualOfficeAddress());
            dataMap.put("enterpriseServeFee", x.getEnterpriseServeFee());
            dataMap.put("billCategoryNameA", x.getBillCategoryNameA());
            dataMap.put("billCategoryNameB", x.getBillCategoryNameB());
            dataMap.put("accountName", x.getAccountName());
            dataMap.put("bankAccountName", x.getBankAccountName());
            dataMap.put("accountNumber", x.getAccountNumber());
            dataMap.put("accountTax", x.getAccountTax());
            dataMap.put("address", x.getAddress());
            dataMap.put("accountPhone", x.getAccountPhone());
            dataMap.put("projectBackground", x.getProjectBackground());
            dataMap.put("businessProcesses", x.getBusinessProcesses());
            dataMap.put("contactorName", x.getContactorName());
            dataMap.put("contactorMobile", x.getContactorMobile());
            // 拆分日期字符串
            String[] date = x.getDate().split("/");
            dataMap.put("year", date[0]);
            dataMap.put("month", date[1]);
            dataMap.put("day", date[2]);
            dataMap.put("levyName", levy.getLevyName());
            dataMap.put("levyContactorName", levy.getLevyContactorName());
            dataMap.put("levyContactorMobile", levy.getLevyContactorMobile());
            dataMap.put("levyContactorMail", levy.getLevyContactorMail());
            dataMap.put("levyAddress", levy.getLevyAddress());
            dataMap.put("serviceType", levy.getServiceType());
            dataMap.put("levyBankAccountName", levy.getLevyBankAccountName());
            dataMap.put("levyAccountNumber", levy.getLevyAccountNumber());
            // 导出
            // 文件路径
            String oldPath = wordUtil.createWord(dataMap);
            if ("文档生成失败".equals(oldPath)){
                log.error("文档生成失败");
            }
            log.info(oldPath);
            stringBuilder.append(oldPath).append(" , ");
        });
        return stringBuilder;
    }

}
